﻿package Gameplay.Visualizer
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.filters.BitmapFilter;
    import flash.filters.BlurFilter;
    import flash.geom.ColorTransform;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.geom.Rectangle;    
    import flash.media.SoundMixer;    
    import flash.utils.ByteArray;    
    import flash.display.Shape;
	
	public class EqualizerVisualizer extends AbstractVisualizer
	{
		private var FLeft:Vector.<EqualizerBar>;
        private var FRight:Vector.<EqualizerBar>;
        
		private var FBackground:Shape;
        private var FCanvas:Bitmap;
        private var FColorTransform:ColorTransform;
        private var FFilter:BitmapFilter;
		
		public function EqualizerVisualizer():void
		{			
			FBackground = new Shape();
			FBackground.graphics.beginFill(0xFFFFFFFF, 1);
			FBackground.graphics.drawRect(0, 0, width, height);
			FBackground.graphics.endFill();
			addChild(FBackground);
			
			FColorTransform = new ColorTransform(0.9, 0.9, 0.9); 
			FFilter = new BlurFilter(8, 4);	
			
			FLeft = new Vector.<EqualizerBar>;
			FRight = new Vector.<EqualizerBar>;
			
			var vBar:EqualizerBar;
            for (var i:uint=0; i<64; i++) 
			{             
                vBar = new EqualizerBar(0xFFFFFF, false);
                vBar.y = i * (EqualizerBar.LENGTH + EqualizerBar.MARGIN) + EqualizerBar.LENGTH + 10;
                vBar.x = (EqualizerBar.LENGTH + EqualizerBar.MARGIN) * EqualizerBar.NUM_RECT;
                FLeft.unshift(vBar);
                vBar = new EqualizerBar(0xFFFFFF, false);
                vBar.y = i * (EqualizerBar.LENGTH + EqualizerBar.MARGIN) + 10;
                vBar.x = Math.floor(width / 2) + 1;
                FRight.unshift(vBar);
            }
                      
            FCanvas = new Bitmap(new BitmapData(width, height, true, 0x00000000));
            addChild(FCanvas);
		}
		
		override protected function OnEnterFrame(e:Event):void
		{
			var vData:ByteArray = new ByteArray();
            var vMatrix:Matrix;
            var vRect:Rectangle = new Rectangle(0, 0, FCanvas.width, FCanvas.height);
            var vPoint:Point = new Point(0, 0);
            
            FCanvas.bitmapData.lock();
			
            SoundMixer.computeSpectrum(vData, true);            
            var vAmplitude:Number = Math.max(FChannel.rightPeak, FChannel.leftPeak) * 0.02;
            FColorTransform.redMultiplier += (0.5 - Math.random()) * vAmplitude;
            FColorTransform.blueMultiplier += (0.5 - Math.random()) * vAmplitude;
            FColorTransform.greenMultiplier += (0.5 - Math.random()) * vAmplitude;
            FColorTransform.redMultiplier = Math.min(Math.max(FColorTransform.redMultiplier, 0.8), 1);
            FColorTransform.blueMultiplier = Math.min(Math.max(FColorTransform.blueMultiplier, 0.8), 1);
            FColorTransform.greenMultiplier = Math.min(Math.max(FColorTransform.greenMultiplier, 0.8), 1);
            FCanvas.bitmapData.colorTransform(vRect, FColorTransform);            
            FCanvas.bitmapData.applyFilter(FCanvas.bitmapData, vRect, vPoint, FFilter);
            var i:uint;
            for (i=0; i<64; i++) 
			{
                FLeft[i].Level = Math.sqrt((vData.readFloat() + vData.readFloat() + vData.readFloat() + vData.readFloat()) / 4);
                vMatrix = new Matrix();
                vMatrix.rotate(Math.PI)
                vMatrix.translate(FLeft[i].x - 20, FLeft[i].y);
                FCanvas.bitmapData.draw(FLeft[i], vMatrix);
            }
            for (i=0; i<64; i++) 
			{
                FRight[i].Level = Math.sqrt((vData.readFloat() + vData.readFloat() + vData.readFloat() + vData.readFloat()) / 4);
                vMatrix = new Matrix();
                vMatrix.translate(FRight[i].x + 20, FRight[i].y);
                FCanvas.bitmapData.draw(FRight[i], vMatrix);
            }
            
            FCanvas.bitmapData.unlock();
		}
	}	
}
